import 'package:sow_app_core/sow_app_core.dart';

import '../usecase/user_usecase.dart';
import '../usecase/token_usecase.dart';
import 'usecase.dart';

class UserLoginUseCase extends UseCase<void, NoParams> {
  final UserRepository _userRepository;
  final LoginPresenter _presenter;
  final TokenRepository _tokenRepository;
  final RestClient _client;

  UserLoginUseCase(this._userRepository, this._presenter, this._tokenRepository,
      this._client);
  @override
  void call(NoParams params) async {
    try {
      final token = await LoginUseCase(_userRepository)(LoginParam(
          _presenter.username!,
          _presenter.password!,
          _presenter.captchaCode,
          _presenter.captchaUuid));
      await (SetTokenUseCase(_tokenRepository, _client)(token));

      _presenter.onComplete();
      _presenter.onPop();
    } on ServerException catch (e) {
      _presenter.onError(e);
    }
  }
}
